#include <fstream>
#include "algo.h"

TextResult::TextResult(std::ostream& os)
{
    file_stream_ = false;
    os_ = &os;
}

TextResult::TextResult(const std::string& fname)
{
    os_ = new std::ofstream(fname.c_str());
    file_stream_ = true;
}

TextResult::~TextResult()
{
    if (file_stream_) {
        dynamic_cast<std::ofstream*>(os_)->close();
        delete os_;
    }
}

std::ostream& TextResult::os()
{
    return *os_;
}

std::stringstream& TextResult::operator() () 
{ 
    return s_; 
}

void TextResult::flush() 
{ 
    os() << s_.str() << ENDL; 
    s_.str(std::string()); 
    s_.clear(); 
}
